<template>
  <el-dialog
    :visible.sync="dialogOpen"
    :title="dialogTitle"
    :before-close="cancel"
    width="30%"
    destroy-on-close
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="110px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="算法类型" prop="algorithmType">
            <el-input v-model="formData.algorithmType" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="版本号" prop="versionNo">
            <el-input v-model="formData.versionNo" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="数据集" prop="dataSetId">
            <el-select
              v-model="formData.dataSetId"
              placeholder="请选择数据集类型"
            >
              <el-option
                v-for="(item, index) in dataSetList"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="训练样本数" prop="trainSampleNum">
            <el-input v-model="formData.trainSampleNum" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="训练样本时间" prop="trainSampleTime">
            <el-input v-model="formData.trainSampleTime" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="版本固化时间" prop="versionFreezeTime">
            <el-input
              v-model="formData.versionFreezeTime"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="模型正确率" prop="accuracy">
            <el-input v-model="formData.accuracy" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="版本描述" prop="description">
            <el-input
              v-model="formData.description"
              type="textarea"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="文件上传" prop="files">
            <el-upload
              class="upload"
              ref="upload"
              name="files"
              :action="upload.url"
              accept=".txt, .xls, .xlsx"
              :before-upload="beforeUpload"
              :on-change="handleChange"
              :on-success="handleSuccess"
              multiple
              :limit="3"
              :file-list="formData.files"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                仅允许导入txt、xls、xlsx格式文件。
              </div>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<style scoped>
.dialog-footer {
  text-align: center;
}
</style>
<script>
import {
  getQueryAllDataSetList,
  addVersion,
  updateVersion,
  testTransform,
} from "@/api/aifactory";

export default {
  name: "Edit",
  props: [
    "dialogTitle", //标题
    "dialogOpen", //显示
    "dialogData",
  ],
  data() {
    return {
      dataSetList: [], //数据集
      formData: {},
      upload: {
        url: process.env.VUE_APP_BASE_API + "/file/batch/upload",
      },
      // 表单校验
      rules: {
        algorithmType: [
          { required: true, message: "算法类型不能为空", trigger: "blur" },
        ],
        versionNo: [
          { required: true, message: "版本号不能为空", trigger: "blur" },
        ],
        dataSetId: [
          { required: true, message: "请选择数据集", trigger: "change" },
        ],
        trainSampleNum: [
          { required: true, message: "版本训练数不能为空", trigger: "blur" },
        ],
        trainSampleTime: [
          { required: true, message: "训练样本时间不能为空", trigger: "blur" },
        ],
        versionFreezeTime: [
          { required: true, message: "版本固化时间不能为空", trigger: "blur" },
        ],
        accuracy: [
          { required: true, message: "模型正确率不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "版本描述不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {},
  created() {},
  mounted() {
    this.init();
  },
  methods: {
    init() {
      console.log("dialogData", this.dialogData);
      this.formData = this.dialogData;
      console.log("formData", this.formData);
      //查询所有数据集
      getQueryAllDataSetList().then((res) => {
        if (res.code == 200) {
          const data = [];
          res.data.forEach((item) => {
            data.push({ label: item.setName, value: item.id });
          });
          this.dataSetList = data;
          console.log("dataSetList:", this.dataSetList);
        }
      });
    },
    // 取消按钮
    cancel() {
      this.$emit("closeDialog");
    },
    // 表单重置
    reset() {
      this.formData = {};
      this.resetForm("form");
    },
    beforeUpload(file) {
      // console.log('beforeUpload', file);
    },
    handleChange(file) {
      // console.log('handleChange', file)
    },
    handleSuccess(res) {
      console.log("handleSuccess", res);
      if (res.code == 200) {
        let files = [];
        if(JSON.parse(this.formData.versionFiles).length > 0){
          files = JSON.parse(this.formData.versionFiles)
        }
        console.log(191, files)
        files.push(res.data[0]);
        console.log("193, files", files);
        this.formData.versionFiles = JSON.stringify(files);
        console.log("versionFiles", this.formData.versionFiles);
      }
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        console.log("submitForm", this.formData);
        if (valid) {
          if (this.formData.id != undefined) {
            console.log(111);
            updateVersion(this.formData).then((response) => {
              console.log(response);
              if (response.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
                this.$emit("closeDialog");
              } else {
                this.$message({ message: response.message, type: "error" });
              }
            });
          } else {
            addVersion(this.formData).then((response) => {
              console.log(1111, response);
              if (response.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
                this.$emit("closeDialog");
              } else {
                this.$message({ message: response.message, type: "error" });
              }
            });
          }
        }
      });
    },
    // 测试数据源
    testPreview() {
      const dataSetParamDtoList = [];
      const dataSetTransformDtoList = [];
      console.log(209, this.formData);
      const data = {
        dataSetParamDtoList: dataSetParamDtoList,
        dataSetTransformDtoList: dataSetTransformDtoList,
        dynSentence: this.formData.dynSentence,
        setType: "mysql",
        sourceCode: this.formData.sourceCode,
      };
      console.log(196, data);
      testTransform(data).then((res) => {
        console.log("testTransform", res);
        if (response.code == 200) {
          this.$message({ message: "操作成功", type: "success" });
        } else {
          this.$message({ message: response.message, type: "error" });
        }
      });
    },
  },
};
</script>
